package cn.easytogo.action.admin;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.easytogo.action.BaseAction;
import cn.easytogo.bean.User;
import cn.easytogo.service.IRoleDAO;
import cn.easytogo.service.IUserDAO;
import cn.easytogo.service.IUsergroupDAO;

public class UserAction  extends BaseAction{
	private IUserDAO userDAO;
	private IRoleDAO roleDAO;
	private IUsergroupDAO usergroupDAO;
	private String username;
	private String password;
	private String email;
	private String realname;
	private String id;
	private String roleIds;
	private String admin;
	private String usergroup_id;
	private List userList;
	private List usergroupList;
	private List roleList;
	/**
	 * 前往用户列表维护页面
	 * */
	public String toList(){
		Map map = new HashMap();
		try {
			userList = userDAO.selectUsersByMap(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_list";
	}
	/**
	 * 前往用户新加页面
	 * */
	public String toNew(){
		initRoleList();
		initUsergroupList();
		return "to_new";
	}
	/**
	 * 保存用户
	 * */
	public void saveNew(){
		User u = new User();
		u.setUsername(username);
		u.setPassword(password);
		u.setEmail(email);
		u.setRealname(realname);
		u.setAdmin(admin);
		u.setRoleIds(roleIds);
		u.setUsergroup_id(usergroup_id);
		try {
			userDAO.insertUser(u);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			print("failed");
		}
	}
	/**
	 * 前往用户修改页面
	 * */
	public String toUpdate(){
		initRoleList();
		initUsergroupList();
		Map map = new HashMap();
		map.put("id", id);
		try {
			List l = userDAO.selectUsersByMap(map);
			if(l!=null&&l.size()>0){
				User u = (User) l.get(0);
				username=u.getUsername();
				password=u.getPassword();
				email=u.getEmail();
				realname=u.getRealname();
				admin=u.getAdmin();
				roleIds=u.getRoleIds();
				usergroup_id=u.getUsergroup_id();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_new";
	}
	/**
	 * 更新用户
	 * */
	public void saveUpdate(){
		if(id!=null&&!"".equals(id)){
			User u = new User();
			u.setId(id);
			u.setUsername(username);
			u.setPassword(password);
			u.setEmail(email);
			u.setRealname(realname);
			u.setAdmin(admin);
			u.setRoleIds(roleIds);
			u.setUsergroup_id(usergroup_id);
			try {
				userDAO.updateUser(u);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 删除用户
	 * */
	public void delete(){
		if(id!=null&&!"".equals(id)){
			User user = new User();
			user.setId(id);
			try {
				userDAO.deleteUser(user);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 初始化角色List
	 * */
	private void initRoleList(){
		Map map = new HashMap();
		try {
			roleList = roleDAO.selectRolesByMap(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 初始化用户组List
	 * */
	private void initUsergroupList(){
		Map map = new HashMap();
		try {
			usergroupList = usergroupDAO.selectUsergroupsWithoutRolesByMap(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public IUserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public IRoleDAO getRoleDAO() {
		return roleDAO;
	}
	public void setRoleDAO(IRoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	public IUsergroupDAO getUsergroupDAO() {
		return usergroupDAO;
	}
	public void setUsergroupDAO(IUsergroupDAO usergroupDAO) {
		this.usergroupDAO = usergroupDAO;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getRoleIds() {
		return roleIds;
	}
	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	public String getAdmin() {
		return admin;
	}
	public void setAdmin(String admin) {
		this.admin = admin;
	}
	public String getUsergroup_id() {
		return usergroup_id;
	}
	public void setUsergroup_id(String usergroupId) {
		usergroup_id = usergroupId;
	}
	public List getUserList() {
		return userList;
	}
	public void setUserList(List userList) {
		this.userList = userList;
	}
	public List getUsergroupList() {
		return usergroupList;
	}
	public void setUsergroupList(List usergroupList) {
		this.usergroupList = usergroupList;
	}
	public List getRoleList() {
		return roleList;
	}
	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}
}
